##############################################################################
#                                                                            #
# Copyright (c) 2008 Brenno de Winter (brenno@dewinter.com)                  #
# Small Sister Project: http://www.smallsister.org/                          #
#                                                                            #
# This program is licensed under the GNU General Public License V3,          #
# the full source code is included in the binary distribution.               #
##############################################################################

#Import systemmodules
import sqlite3

def opendatabase(): 
    #Open the database, create the cursor
    connection = sqlite3.connect("smallsister.db")
    cursor = connection.cursor()
    
    #Create the tables if they don't exist
    updateversion(cursor)
    
    return cursor
    
def updateversion(cursor):
    
    #Create the initial database (if it doesn't exist
    querystring = "CREATE TABLE IF NOT EXISTS contact "
    querystring = querystring + "(contactid VARCHAR(50) PRIMARY KEY, "
    querystring = querystring + "trustlevel INTEGER, firstname VARCHAR(255), "
    querystring = querystring + "lastname VARCHAR(255), openpgpkeyid VARCHAR(50))"
    cursor.execute (querystring)
    
    querystring = "CREATE TABLE IF NOT EXISTS emailaddress "
    querystring = querystring + "(contactid VARCHAR(50), email VARCHAR(255))"
    cursor.execute (querystring)
    
    querystring = "CREATE TABLE IF NOT EXISTS destination "
    querystring = querystring + "(destinationid VARCHAR(50), gatewayid VARCHAR(50))"
    cursor.execute (querystring)
    
